//
//  LRNavController.swift
//  WeiboX
//
//  Created by admin on 30/11/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit

class LRNavController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
		interactivePopGestureRecognizer?.delegate = self
		
    }
	
	override func pushViewController(_ viewController: UIViewController, animated: Bool) {
//		print(viewControllers.count)
		
		if viewControllers.count > 0 {
			// 隐藏底部 tabbar
			viewController.hidesBottomBarWhenPushed = true
		}
		
		super.pushViewController(viewController, animated: animated)
		
		if viewControllers.count > 1 {
			if viewControllers.count == 2 {
				let title = viewControllers.first!.title!
				
				viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imgName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
			} else {
				viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "back", imgName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
			}
			viewController.title = "第\(viewControllers.count)级"
		}
	}
	
	@objc func popAction() {
		popViewController(animated: true)
	}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension LRNavController: UIGestureRecognizerDelegate {
	func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
		if viewControllers.count == 1 {
			return false
		}
		return true
	}
}
